#include "latex.h"
#include "utilities.h"
#include "solver.h"
#include <fstream>


void ScacchieraLaTeX(std::ofstream& f, Scacchiera& s)
{
    f << "\n\\begin{sudoku}\n";
    for (int y=1;y<=Y;++y)
    {
	for (int x=1;x<=X;++x)
	    f << "|" << (s.valore(x,y)==0?' ':s.valore(x,y));
	f << "|.\n";
    }
    f << "\\end{sudoku}" << std::flush;
}

void ScacchieraLaTeX(std::ofstream& f, char* s)
{
    f << "\n\\begin{sudoku}\n";
    for (int y=0;y<Y;++y)
    {
	for (int x=0;x<X;++x)
	    f << "|" << (s[y*X+x]=='0'?' ':s[y*X+x]);
	f << "|.\n";
    }
    f << "\\end{sudoku}" << std::flush;
}

void ScacchieraLaTeX(char* filename, char* s)
{
    std::ofstream f;
    f.open(filename);
    ScacchieraLaTeX(f,s);
    f.close();
}

void ScacchieraLaTeX(char* filename, Scacchiera& s)
{
    std::ofstream f;
    f.open(filename);
    ScacchieraLaTeX(f,s);
    f.close();
}

void FileLaTeX(char* filename, char* sudoku)
{
    std::ofstream f;
    f.open(filename);
    f << "%Automatically generated by sudoku <giurrero@gmail.com>";
    f << "\n%Package sudoku.sty is required to renderize";
    f << "\n\n\\documentclass{article}";
    f << "\n\\usepackage{sudoku}";
    f << "\n\\usepackage[utf-8]{inputenc}";
    f << "\n\\title{Sudoku}";
    f << "\n\n\\begin{document}\n";
    f << "\n\\maketitle";
    f << "\nAutomatic generated sudokus by sudoku\\\\";
    int nsudoku = contasudoku(sudoku);
    f << "\n Number of sudokus: " << nsudoku;
    for (int n=0;n<nsudoku;++n)
    {
	f << "\n\\begin{figure}[htp]";
	char *stringa =  sudoku_n(sudoku, n);
	ScacchieraLaTeX(f,stringa);
	Solver a(stringa);	
	f << "\n\\caption{Sudoku number " << n+1 << ", difficulty " << a.difficolta() << "}";
	f << "\n\\end{figure}\n";
	f << "\n\\clearpage";
	delete[] stringa;
    }
    f << "\n\\end{document}";
    f.close();    
}
